<?php 

$user = new User();

function change_profile_image($file_temp, $file_extn){
	 
	 $file_path = 'uploaded/profile/' . substr(md5(time()), 0, 10) . '.' .$file_extn;
	 $profile_data = substr(md5(time()), 0, 10) . '.' .$file_extn;
	
	move_uploaded_file($file_temp, $file_path);

	$user = new User();
	$userID = $user->data()->id;
	$user = DB::getInstance()->update('users', $userID, array(
					
	'profile' => $profile_data
	
	));

	Redirect::to('profile.php');
	exit();
}


// uploading validation
if (isset($_FILES['file']) === true) {
	if (empty($_FILES['file']['name']) === true) {
		echo "<div class='input_error'> Please choose a picture. </div>";
	}else {
		//echo "OK";
		$allowed = array('jpg', 'jpeg', 'gif', 'png');
		$file_name = $_FILES['file']['name'];
		$value  = explode('.', $file_name);
		$file_extn = strtolower(end($value));
		$file_temp = $_FILES['file']['tmp_name'];

		if (in_array($file_extn, $allowed) === true) {
				
				change_profile_image($file_temp, $file_extn);

			}else {
				echo "<div class='input_error'>Incorrect file type, the allowed file types are " . implode(', ', $allowed) . " </div> ";
			}	
	}	
}